/**
 * Created by XianTao on 2015/3/12
 *
 * Copyright (c) 2015-2015
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 */
package com.taotao.wechat.game;

import com.rop.client.CompositeResponse;
import com.taotao.admin.MyRopClient;
import com.taotao.rop.database.domain.PlayGame;
import com.taotao.rop.response.BaseResponse;
import com.taotao.rop.util.DateTimeUtil;
import com.taotao.wechat.message.resp.Article;
import com.taotao.wechat.message.resp.BaseMessage;
import com.taotao.wechat.message.resp.NewsMessage;
import com.taotao.wechat.util.MessageUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class SlotGame extends Game {
    public static final boolean IS_ENABLE = true;
    private static final String game_url_format = "/game/slot/index.html?appId=%s&openId=%s&tel=%s";

    public SlotGame() {}

    public SlotGame(BaseMessage baseMessage, String content, String appKey) {
        super(baseMessage, content, appKey);
    }

    @Override
    protected String gameUrl() {
        String url = MyRopClient.BASE_URL + String.format(game_url_format, appKey, baseMessage.getToUserName(), content);
        NewsMessage newsMessage = new NewsMessage(baseMessage);
        newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
        newsMessage.setArticleCount(1);
        Article article = new Article();
        article.setDescription("老虎机的描述");
//        article.setPicUrl(MyRopClient.BASE_URL + "/game/wheel2/resource/zt.jpg");
        article.setTitle("老虎机的标题");
        article.setUrl(url);
        List<Article> articles = new ArrayList<Article>();
        articles.add(article);
        newsMessage.setArticles(articles);
        return MessageUtil.newsMessageToXml(newsMessage);
    }

    @Override
    public String getAppIds() {
        return "wx8010a0acb61a2948";
    }

    @Override
    public String getMemberCode() {
        return null;
    }

    @Override
    public Integer getMemberStatus() {
        return null;
    }

    @Override
    public String getName() {
        return "517";
    }

    @Override
    public String getNickName() {
        return "slot";
    }

    @Override
    public String getStartDate() {
        return "2015-03-12";
    }

    @Override
    public String getEndDate() {
        return "2015-05-25";
    }

    @Override
    public String wechatJoinCountToday(String openId, String appKey) {
        //判断是否在活动的日子
        Date now = new Date(DateTimeUtil.getDateStartMillis(new Date()));
        String activeDay = getActiveDay();
        if (activeDay == null || DateTimeUtil.compareDate(activeDay, now) != 0) {
            return msgNotInActiveDay();
        }

        PlayGame condition = new PlayGame();
        condition.setAppId(appKey);
        condition.setOpenId(openId);
        condition.setGameName(getNickName());

        CompositeResponse<BaseResponse> response = MyRopClient.getRopClient("").buildClientRequest()
                .post(condition, BaseResponse.class, "playGame.getJoinCountToday", "1.0");
        if (response.isSuccessful()) {
            BaseResponse baseResponse = response.getSuccessResponse();
            if (baseResponse.getSuccess()) {
                int count = (Integer) baseResponse.getData();
                if (count >= 1) {
                    return msgAlreadyJoin();
                } else {
                    return "";
                }
            }
        }
        return "系统繁忙，请您稍后再试";
    }

    @Override
    public String telJoinCountToday(String tel, String openId, String appKey) {
        //判断是否在活动的日子
        Date now = new Date(DateTimeUtil.getDateStartMillis(new Date()));
        String activeDay = getActiveDay();
        if (activeDay == null || DateTimeUtil.compareDate(activeDay, now) != 0) {
            return msgNotInActiveDay();
        }

        PlayGame condition = new PlayGame();
        condition.setTel(tel);
        condition.setGameName(getNickName());

        CompositeResponse<BaseResponse> response = MyRopClient.getRopClient("").buildClientRequest()
                .post(condition, BaseResponse.class, "playGame.getTelJoinCountToday", "1.0");
        if (response.isSuccessful()) {
            BaseResponse baseResponse = response.getSuccessResponse();
            if (baseResponse.getSuccess()) {
                int count = (Integer) baseResponse.getData();
                if (count >= 1) {
                    return msgTelAlreadyJoin();
                } else {
                    return "";
                }
            }
        }
        return "系统繁忙，请您稍后再试";
    }

    public List<String> getActiveDays() {
        return Arrays.asList("2015-03-17", "2015-03-19", "2015-03-21", "2015-03-23",
                "2015-03-25", "2015-03-27", "2015-03-28", "2015-03-30", "2015-04-01", "2015-04-03");
//        return Arrays.asList("2015-04-24", "2015-04-27", "2015-05-01", "2015-05-04",
//                "2015-05-08", "2015-05-11", "2015-05-17", "2015-05-18", "2015-05-22", "2015-05-25");
    }

    public String getActiveDay() {
        List<String> activeDays = getActiveDays();
        Date now = new Date(DateTimeUtil.getDateStartMillis(new Date()));
        for (String activeDay : activeDays) {
            if (DateTimeUtil.compareDate(activeDay, now) == 0) {
                return activeDay;
            }
        }
        return null;
    }

    public String getNextActiveDay() {
        List<String> activeDays = getActiveDays();
        Date now = new Date(DateTimeUtil.getDateStartMillis(new Date()));
        for (String activeDay : activeDays) {
            if (DateTimeUtil.compareDate(activeDay, now) > 0) {
                return activeDay;
            }
        }
        return null;
    }

    public String msgNotInActiveDay() {
        String nextActiveDay = getNextActiveDay();
        if (nextActiveDay == null) {
            return "本日非活动日，请您先阅读活动规则~";
        } else {
            return "本日非活动日，请您于" + nextActiveDay + "再来参与~";
        }
    }

    public String msgAlreadyJoin() {
        String nextActiveDay = getNextActiveDay();
        if (nextActiveDay == null) {
            return "今日机会已用完，请耐心等待奖品到账~";
        } else {
            return "今日机会已用完，请您于" + nextActiveDay + "再来参与~";
        }
    }

    public String msgTelAlreadyJoin() {
        return "此手机号码已经被别人使用，请更换手机号码参与活动。";
    }

    @Override
    public String getMsgGameNotStart() {
        return "亲您也太心急了，我们的【" + getName() + "】活动还没有开始呢！";
    }

    @Override
    public String getMsgGameIsEnd() {
        return "【" + getName() + "】活动已经结束。";
    }

    @Override
    public String getMsgAfterGameName() {
        return "请输入内蒙移动手机号码参与【" + getName() + "】游戏";
    }
}
